package app.qrcode.browser.chat.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import app.qrcode.browser.chat.R
import com.hyphenate.chat.EMClient
import kotlinx.android.synthetic.main.invite_item.view.*

/**
 * Created by DomenCai on 2022/3/15.
 */
class InviteInfoAdapter : RecyclerView.Adapter<InviteInfoAdapter.InviteInfoViewHolder>() {
    private val inviteList: MutableList<InviteInfo> = mutableListOf()

    fun updateList(newList: List<InviteInfo>) {
        inviteList.clear()
        inviteList.addAll(newList)
        notifyDataSetChanged()
    }

    inner class InviteInfoViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val invitePhoto: ImageView = view.findViewById(R.id.invitePhoto)
        val inviteName: TextView = view.findViewById(R.id.inviteName)
        val agreeBtn: Button = view.findViewById(R.id.agreeButton)
        val inviteReason: TextView = view.findViewById(R.id.inviteReason)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): InviteInfoAdapter.InviteInfoViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.invite_item, parent, false)
        val viewHolder = InviteInfoViewHolder(view)
        viewHolder.agreeBtn.setOnClickListener {
            EMClient.getInstance().contactManager().acceptInvitation(it.agreeButton.text.toString())
        }
        return viewHolder
    }

    override fun onBindViewHolder(holder: InviteInfoAdapter.InviteInfoViewHolder, position: Int) {
        val inviteInfo = inviteList[position]
        holder.invitePhoto.setImageResource(inviteInfo.type)
        holder.inviteName.text = inviteInfo.hxId
        holder.inviteReason.text = inviteInfo.reason
        holder.agreeBtn.text=inviteInfo.agreeStatus
    }

    override fun getItemCount(): Int = inviteList.size
}